﻿/*
 * 
 * définit un objet physique pouvant etre utilisé dans Engine. A la base, contient une plateforme pour poser les pieds.
 * 
 * Lie un DisplayObject et son segment de contact.
 * 
 * */
package physic
{
	import bipede.membres.Person;
	import drawer.events.DrawEvent;
	import events.MoveEvent;
	import events.PlateformeEvent;
	import fab.Utils;
	import flash.display.DisplayObject;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.geom.Matrix;
	
	import fab.geom.*;
	import bipede.membres.Pied;
	
	public class Obstacle extends EventDispatcher
	{
		public var display:DisplayObject;
		public var p1:Point;
		public var p2:Point;
		public var _plateforme:Segment;
		//public var bounds:Rectangle;
		static public var liste:Array = new Array();
		
		public function Obstacle( _display:DisplayObject, p1:Point, p2:Point )
		{
			//p1 = new Point( 0, 400 );
			//p2 = new Point( 800, 400 );
			
			display = _display;
			this.p1 = p1;// new Point( 0, 0 );
			this.p2 = p2;//p2 = new Point( display.width / display.scaleX, 0 );
			
			_plateforme = new Segment( p1, p2 );
			liste.push( this );
			
			display.addEventListener( MoveEvent.MOVING, updatePlateformeDragger );
		}
		static public function traceZoneObstacle():void
		{		
			//var b:Rectangle;
			//var mat:Matrix;
			//zone_obstacles = new BitmapData(900,600, true, 0x00000000);
			//for each ( var obstacle:Obstacle in liste )
			//{
				//b = obs.zone.getBounds( obs );
				//mat = new Matrix();
				//mat.scale( obstacle.zone.scaleX, obs.zone.scaleY );
				//mat.translate( b.x, b.y );
				//mat.scale( obstacle.display.scaleX, obstacle.display.scaleY );
				//mat.translate( obstacle.display.x, obstacle.display.y );
				//
				//zone_obstacles.draw( obstacle.display, mat );
			//}
		}
		/*
		 * renvoie un tableau d'object { intersect:Point intersection, obstacle:Obstacle touché }
		 * */
		static public function hitSegment( s:Segment ):Array
		{		
			var intersect_array:Array = new Array();
			var intersect:Point;
			var plateforme:Segment;
			//dispatchEvent( new DrawEvent( DrawEvent.DRAWSEGMENT, s.debut, s.fin, 0xFF0000 ) );
			for each ( var obstacle:Obstacle in liste )
			{
				plateforme = obstacle.plateforme;
				intersect = Segment.instersection( obstacle.plateforme, s );
				if ( intersect != null ) intersect_array.push( { intersect:intersect, obstacle:obstacle } );
			}
			return intersect_array;
		}			
		static public function hit( p:Point ):Obstacle
		{		
			for each ( var obstacle:Obstacle in liste ) if ( obstacle.display.hitTestPoint( p.x, p.y, true ) ) return obstacle;
			return null;
		}
		private function updatePlateformeDragger(event:MoveEvent):void 
		{
			trace("updatePlateformeDragger");
			//updatePlateforme( new PlateformeEvent( PlateformeEvent.UPDATE, displayevent.point,
		}		
		public function updatePlateforme( event:PlateformeEvent ):void
		{
			// TODO deplacement de déformation avec plateforme.pointAt
			//var deplacement:Point = event.debut.subtract( _plateforme.debut );
			//_plateforme.update( _plateforme.debut.add( event.debut ), _plateforme.fin.add( event.debut ) );

			//dispatchEvent( new DrawEvent( DrawEvent.DRAWSEGMENT, plateforme, 0xFF00FF ) );
			
			var a:Array = Liaison.getPieds( this );
			if ( a.length > 0 )
			{
				var liaison:Liaison = a[ 0 ] as Liaison;
				liaison.obstacle_moving( event.deplacement );
			}
		}		
		public function get plateforme():Segment
		{
			return Utils.SegmentLocalTransformation( _plateforme, display, Engine.espace );
		}			
		override public function toString():String
		{
			return "Obstacle : " + display.name;
		}			
	}	
}